// pages/detail/detail.js
var app = getApp()
var globalData = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    docData: {},
    isfav: false,
    url: '',
    auth_detail: {},
    imgRoad: '',
    download_progress: '0rpx',
    isloading: false,
    isloaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      docData: JSON.parse(options.detail)
    })
    this.isfaved()
    let res = await app.get(`users/${this.data.docData.user}/`)
    this.setData({
      auth_detail: res.data,
      isloaded: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  async changeFav() {
    let res = await app.changeFav(this.data.isfav, this.data.docData)
    this.setData({
      isfav: this.data.isfav ^ res
    })
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('changefav', this.data.isfav)
  },

  getdownload() {
    this.itisafunc().then(async () => {
      let res = await app.post(`downloads/${this.data.docData.id}/`)
      console.log(res)
      this.setData({
        isloading: false
      })
    })
  },

  //判断是否被收藏
  isfaved() {
    this.setData({
      isfav: false
    })
    const fav = app.globalData.favorities
    fav.map((data) => {
      if (this.data.docData.id == data.id) {
        this.setData({
          isfav: true
        })
      }
    })
  },

  itisafunc() {
    return new Promise((resolve, reject) => {
      const downloading = wx.downloadFile({
        url: this.data.docData.address,
        success: (res) => {
          this.setData({
            url: res.tempFilePath || res.filePath
          })
          if (app.globalData.system.indexOf('Windows') == 0 || app.globalData.system.indexOf('macOS') == 0) {
            wx.saveFileToDisk({
              filePath: this.data.url,
              success: (res) => {
                resolve()
              }
            })
          } else if (app.globalData.system.indexOf('Android') == 0 || app.globalData.system.indexOf('iOS') == 0) {
            wx.openDocument({
              filePath: this.data.url,
              success: (res) => {
                console.log('打开成功')
                resolve()
              }
            })
          }


        }
      })
      this.setData({
        isloading: true
      })
      downloading.onProgressUpdate((res) => {
        this.setData({
          download_progress: res.progress * 6.3 + 'rpx'
        })
      })
    })
  },

  async getconnect() {
    let res = await wx.showActionSheet({
      itemList: ['微信：' + this.data.auth_detail.WeChat, 'QQ：' + this.data.auth_detail.QQ],
    })
    let arr = [this.data.auth_detail.WeChat, this.data.auth_detail.QQ]
    wx.setClipboardData({
      data: arr[res.tapIndex]
    })
  },

  onUnload() {

  }

})